package com.ssmoa.fm.springmvc.freemarker.lab;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.ssmoa.utils.SSIConstants;
import com.ssmoa.utils.SSIUtils;

import freemarker.core.Environment;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

@Component
public class JSTag implements TemplateDirectiveModel {

	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		SimpleScalar type = (SimpleScalar) params.get("type");
		SimpleScalar selfjs = (SimpleScalar) params.get("selfjs");
		SimpleScalar selfcs = (SimpleScalar) params.get("selfcs");
		Writer out = env.getOut();
		String base = System.getProperty(SSIConstants.CONTENTPATH);
		StringBuilder sb = new StringBuilder();
		if(type!=null){
			sb.append("<link href=\""+base+"/res/css/base.css\" rel=\"stylesheet\" type=\"text/css\"/>");
			sb.append("<script src=\""+base+"/res/js/lib/jquery.min.js\" type=\"text/javascript\"></script>");
			sb.append("<script src=\""+base+"/resicon.css\" rel=\"stylesheet\" type=\"text/css\"/>");
				sb.append("<script src=\""+base+"/res/js/lib/lhgDialog/lhgdialog.min.js?skin=iblue\" type=\"text/javascript\"></script>");
			sb.append("<script src=\""+base+"/res/js/lib/commonSet.js\" type=\"text/javascript\"></script>");
			//sb.append("<script src=\""+base+"/res/js/lib/base.js\" type=\"text/javascript\"></script>");
			String[] types = type.getAsString().split(",");
			if(SSIUtils.isIn("jqueryui", types)){
//				sb.append("<script type=\"text/javascript\" src=\"plug-in/tools/dataformat.js\"></script>");
				sb.append("<link href=\""+base+"/res/css/default/easyui.css\" rel=\"stylesheet\" type=\"text/css\"/>");
				sb.append("<link href=\""+base+"/res/css/icon.css\" rel=\"stylesheet\" type=\"text/css\"/>");
				sb.append("<script src=\""+base+"/res/js/lib/jquery.easyui.min.js\" type=\"text/javascript\"></script>");
				sb.append("<script src=\""+base+"/res/js/lib/curdtools.js\" type=\"text/javascript\"></script>");
				sb.append("<script src=\""+base+"/res/js/lib/local/easyui-lang-zh_CN.js\" type=\"text/javascript\"></script>");
			}
			if(SSIUtils.isIn("ztree", types)){
				sb.append("<link href=\""+base+"/res/css/zTreeStyle/zTreeStyle.css\" rel=\"stylesheet\" type=\"text/css\"/>");
				sb.append("<script src=\""+base+"/res/js/lib/jquery.ztree.core-3.5.js\" type=\"text/javascript\"></script>");
				sb.append("<script src=\""+base+"/res/js/lib/jquery.ztree.excheck-3.5.min.js\" type=\"text/javascript\"></script>");
				
			}
			if(SSIUtils.isIn("960", types)){
				sb.append("<link href=\""+base+"/res/css/960_24_col.css\" rel=\"stylesheet\" type=\"text/css\"/>");
			}
		}
		if(selfcs!=null){
			sb.append("<link href=\""+base+"/res/css/"+selfcs.getAsString()+"\" rel=\"stylesheet\" type=\"text/css\"/>");
		}
		if(selfjs!=null){
			sb.append("<script src=\""+base+"/res/js/"+selfjs.getAsString()+"\" type=\"text/javascript\"></script>");
		}
		out.write(sb.toString());
	}

}
